package com.hyc.netty.client.handler;

import com.hyc.message.RpcResponse;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.util.AttributeKey;

import java.util.concurrent.CompletableFuture;

/**
 * @author 冷环渊
 * @date 2025/6/6 19:01
 * @description NettyRpcClientHandler
 */
public class NettyRpcClientHandler extends SimpleChannelInboundHandler<RpcResponse> {

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, RpcResponse response) throws Exception {
        AttributeKey<CompletableFuture<RpcResponse>> responseKey = AttributeKey.valueOf("rpcResponse" + response.getRequestId());
        Channel channel = ctx.channel();
        CompletableFuture<RpcResponse> future = channel.attr(responseKey).get();
        if (future != null) {
            future.complete(response);
        }
    }
}
